package com.cl.code.database.mybatis.module.sql.method;

import com.cl.code.database.mybatis.constant.CLMybatisConstants;
import com.cl.code.database.mybatis.module.sql.table.TableInfo;
import com.cl.code.database.mybatis.util.SqlScriptUtils;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.parsing.GenericTokenParser;
import org.apache.ibatis.session.Configuration;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class CLDelete extends AbstractSqlMethod {

    public CLDelete(Class<?> type, Method method, TableInfo tableInfo, Configuration configuration) {
        super(type, method, tableInfo, configuration);
    }

    @Override
    public SqlCommandType getSqlCommandType() {
        return SqlCommandType.DELETE;
    }

    @Override
    public Class<?> getResultType() {
        return Integer.class;
    }

    @Override
    public String buildSql() {
        String sql = "DELETE FROM #{tn} #{where}";
        Map<String, String> map = new HashMap<>();
        map.put("tn", tableInfo.getTableName());
        map.put("where", getWhereSql());
        sql = new GenericTokenParser(CLMybatisConstants.PARAM_START, CLMybatisConstants.PARAM_END, map::get).parse(sql);
        return SqlScriptUtils.convertScript(sql);
    }

}
